package com.toogu.mazhangmen.ui.monitor;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.bean.QATypeBean;
import com.toogu.mazhangmen.ui.login.LoginModel;
import com.toogu.mazhangmen.util.NetWorkUtil;
import com.toogu.mazhangmen.util.OkhttpUtil;
import com.toogu.mazhangmen.util.PopUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ControlModel implements ControlContract.IModel{
    private Activity activity = null;
    private Delegate delegate;
    private String lastMessage = null;
    private Timer mTimer;
    private String projectId;
    private String doorId;
    public ControlModel(Activity activity){
        this.activity=activity;

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onResume() {
      if((!TextUtils.isEmpty(projectId))&&(!TextUtils.isEmpty(doorId))){
          mTimer= new Timer();
          mTimer.schedule(new TimerTask() {
              @Override
              public void run() { getAccessOutRecordBean(projectId,doorId);
              }
          }, 0, 2000);
      }
    }

    @Override
    public void onPause() {
        if(mTimer!=null) mTimer.cancel();
    }

    @Override
    public void onDestory() {
        if(mTimer!=null) mTimer.cancel();
    }

    private void getAccessOutRecordBean(String projectId,String doorId) {
        NetWorkUtil.getNetWorkUtil().getAccessOutRecord(activity,projectId, doorId, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if(activity==null||activity.isFinishing()) {
                    if(mTimer!=null) mTimer.cancel();
                    return;
                }
                Log.d("getAccessOutRecord", "onFailure: "+e.getMessage());
                activity.runOnUiThread(() -> delegate.onDataChanged(null));//月卡的情况
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(activity==null||activity.isFinishing()) {
                    if(mTimer!=null) mTimer.cancel();
                    return;
                }
                String responseText = response.body().string();
                if (response.isSuccessful()&&!responseText.equals(lastMessage)) {
                    lastMessage=responseText;
                    Log.d("getAccessOutRecordBean", "onResponse: "+responseText);
                    ControlContract.AccessOutRecordBean accessOutRecordBean = null;
                    try {
                        accessOutRecordBean = new Gson().fromJson(responseText, ControlContract.AccessOutRecordBean.class);
                    }catch (Exception e){
                    }
                    ControlContract.AccessOutRecordBean finalAccessOutRecordBean = accessOutRecordBean;
                    activity.runOnUiThread(() -> delegate.onDataChanged(finalAccessOutRecordBean));//月卡的情况
                }
            }
        });
    }

    @Override
    public void getTempRecord(String mobile,String projectId) {
        NetWorkUtil.getNetWorkUtil().getTempRecord(activity,mobile, projectId, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                activity.runOnUiThread(()-> Toast.makeText(activity,"获取收费项目失败："+e.getMessage(),Toast.LENGTH_SHORT).show());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseText = response.body().string();
                ControlContract.TempRecord tempRecord = new Gson().fromJson(responseText, ControlContract.TempRecord.class);
                activity.runOnUiThread(()->delegate.showPay(tempRecord));
            }
        });
    }

    @Override
    public void setPersonShow(String projectId, String doorId) {
        this.projectId=projectId;
        this.doorId=doorId;
        if(mTimer!=null) mTimer.cancel();
        mTimer= new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() { getAccessOutRecordBean(projectId,doorId);
            }
        }, 0, 2000);
    }

    @Override
    public void getQAType() {
        NetWorkUtil.getNetWorkUtil().getQAType(activity, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                activity.runOnUiThread(()-> Toast.makeText(activity,"网络连接失败："+e.getMessage(),Toast.LENGTH_SHORT).show());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseText = response.body().string();
                Log.d("QAType", "onResponse: " + responseText);
                List<QATypeBean> qaTypeBeanList = new Gson().fromJson(responseText, new TypeToken<List<QATypeBean>>() {}.getType());
                activity.runOnUiThread(()->delegate.showQA(qaTypeBeanList));
            }
        });


    }


    @Override
    public void setModelDelegate(Delegate delegate) {
        this.delegate=delegate;
    }

}
